package learn1

import com.rabbitmq.client.AMQP
import com.rabbitmq.client.CancelCallback
import com.rabbitmq.client.ConnectionFactory
import com.rabbitmq.client.DeliverCallback
import com.rabbitmq.client.Delivery

/**

 @author HSH

 @create 2022-01-06 10:00

 */
class Consumer {
    final static QUEUE_NAME = "hello"

    static void main(String[] args) {
        ConnectionFactory factor = new ConnectionFactory()
        factor.host = "127.0.0.1"
        factor.username = "admin"
        factor.password = "admin"
        def connection = factor.newConnection()
        def channel = connection.createChannel()
        println "等待接收消息"
        /***
         * 消费者消费消息
         * 1.消费那个队列
         * 2.消费成功之后是否要自动应答 true自动应答 false手动应答
         * 3.消费者未成功消费的回调
         */
        channel.basicConsume(QUEUE_NAME,true,new DeliverCallback(){
            @Override
            void handle(String consumerTag, Delivery message) throws IOException {
                println "消息是${new String(message.getBody())}"
            }
        },new CancelCallback(){

            @Override
            void handle(String consumerTag) throws IOException {
                println "消息消息消费被中断"
            }
        })
    }
}
